package com.ruoyi.quartz.task;

import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.SysCheckIn;
import com.ruoyi.system.service.ISysCheckInService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 老人状态定时任务
 * 
 * @author ruoyi
 */
@Component("elderStatusTask")
public class ElderStatusTask
{
    private static final Logger log = LoggerFactory.getLogger(ElderStatusTask.class);
    
    @Autowired
    private ISysCheckInService checkInService;
    
    @Autowired
    private ISysUserService userService;
    
    /**
     * 更新老人入住状态
     */
    public void updateElderCheckInStatus()
    {
        log.info("开始执行更新老人入住状态任务");
        try {
            // 查询所有状态为在院的入住记录
            SysCheckIn checkInQuery = new SysCheckIn();
            checkInQuery.setStatus("0"); // 在院状态
            List<SysCheckIn> checkInList = checkInService.selectSysCheckInList(checkInQuery);
            
            log.info("查询到{}条入住记录需要处理", checkInList.size());
            
            Date now = new Date();
            for (SysCheckIn checkIn : checkInList) {
                
                // 如果审核状态为已通过
                if ("1".equals(checkIn.getAuditStatus())) {
                    // 更新用户状态为入住中
                    SysUser user = new SysUser();
                    user.setUserId(checkIn.getElderUserId());
                    user.setStatus("1"); // 入住中
                    userService.updateUserStatus(user);
                    log.info("已更新用户{}的状态为入住中", checkIn.getElderUserId());
                }
            }
            log.info("老人入住状态更新任务执行完成");
        } catch (Exception e) {
            log.error("执行老人入住状态更新任务时发生错误", e);
        }
    }
} 